package com.zenithmind.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.common.result.Result;
import com.zenithmind.exam.pojo.entity.ExamCategory;
import com.zenithmind.exam.service.ExamCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 考试分类控制器
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Slf4j
@RestController
@RequestMapping("/api/exam/category")
@RequiredArgsConstructor
@Validated
@Tag(name = "考试分类管理", description = "考试分类的增删改查操作")
public class ExamCategoryController {

    private final ExamCategoryService examCategoryService;

    @PostMapping
    @Operation(summary = "创建考试分类", description = "创建新的考试分类")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public Result<Boolean> createCategory(@Valid @RequestBody ExamCategory category) {
        boolean result = examCategoryService.createCategory(category);
        return Result.success(result);
    }

    @PutMapping("/{categoryId}")
    @Operation(summary = "更新考试分类", description = "更新指定的考试分类")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public Result<Boolean> updateCategory(
            @Parameter(description = "分类ID") @PathVariable @NotBlank String categoryId,
            @Valid @RequestBody ExamCategory category) {
        category.setId(categoryId);
        boolean result = examCategoryService.updateCategory(category);
        return Result.success(result);
    }

    @DeleteMapping("/{categoryId}")
    @Operation(summary = "删除考试分类", description = "删除指定的考试分类")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public Result<Boolean> deleteCategory(
            @Parameter(description = "分类ID") @PathVariable @NotBlank String categoryId) {
        boolean result = examCategoryService.deleteCategory(categoryId);
        return Result.success(result);
    }

    @GetMapping("/{categoryId}")
    @Operation(summary = "获取考试分类详情", description = "根据ID获取考试分类详情")
    public Result<ExamCategory> getCategoryById(
            @Parameter(description = "分类ID") @PathVariable @NotBlank String categoryId) {
        ExamCategory category = examCategoryService.getCategoryById(categoryId);
        return Result.success(category);
    }

    @GetMapping("/children/{parentId}")
    @Operation(summary = "获取子分类列表", description = "根据父分类ID获取子分类列表")
    public Result<List<ExamCategory>> getChildCategories(
            @Parameter(description = "父分类ID") @PathVariable @NotBlank String parentId) {
        List<ExamCategory> categories = examCategoryService.getChildCategories(parentId);
        return Result.success(categories);
    }

    @GetMapping("/root")
    @Operation(summary = "获取根分类列表", description = "获取所有根分类列表")
    public Result<List<ExamCategory>> getRootCategories() {
        List<ExamCategory> categories = examCategoryService.getRootCategories();
        return Result.success(categories);
    }

    @GetMapping("/tree")
    @Operation(summary = "获取分类树", description = "获取完整的分类树结构")
    public Result<List<ExamCategory>> getCategoryTree() {
        List<ExamCategory> categories = examCategoryService.getCategoryTree();
        return Result.success(categories);
    }

    @GetMapping("/enabled")
    @Operation(summary = "获取启用的分类列表", description = "获取所有启用状态的分类列表")
    public Result<List<ExamCategory>> getEnabledCategories() {
        List<ExamCategory> categories = examCategoryService.getEnabledCategories();
        return Result.success(categories);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询考试分类", description = "分页查询考试分类列表")
    public Result<IPage<ExamCategory>> getCategoryPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "分类名称") @RequestParam(required = false) String name,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "父分类ID") @RequestParam(required = false) String parentId) {
        
        Page<ExamCategory> page = new Page<>(pageNum, pageSize);
        IPage<ExamCategory> result = examCategoryService.getCategoryPage(page, name, status, parentId);
        return Result.success(result);
    }

    @PutMapping("/{categoryId}/status")
    @Operation(summary = "更新分类状态", description = "启用或禁用考试分类")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public Result<Boolean> updateCategoryStatus(
            @Parameter(description = "分类ID") @PathVariable @NotBlank String categoryId,
            @Parameter(description = "状态") @RequestParam Integer status) {
        boolean result = examCategoryService.updateCategoryStatus(categoryId, status);
        return Result.success(result);
    }

    @GetMapping("/{categoryId}/can-delete")
    @Operation(summary = "检查分类是否可删除", description = "检查指定分类是否可以删除")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public Result<Boolean> canDeleteCategory(
            @Parameter(description = "分类ID") @PathVariable @NotBlank String categoryId) {
        boolean result = examCategoryService.canDeleteCategory(categoryId);
        return Result.success(result);
    }
}
